#include "pixel.h"

pixel::pixel()
{

}

bool pixel::isPixelBlack(int row, int col, QImage *img)
{
    if (col < 0 || col > img->width()-1 || row < 0 || row > img->height()-1)
        return false;
    return (qGray(img->pixel(QPoint(col,row))) < 128);
}

void pixel::paintPixel(int row, int col, int pixelProportion, QRgb color, QImage *img)
{
    int x;
    if (pixelProportion > 1)
        x = pixelProportion-2;
    else
        x = 0;
    if (col-x > 0 & col+x < img->width()-x & row-x > 0 & row+x < img->height()-x)
    {
        if (x > 0)
        {
            for (int i = -x; i <= x; i++)
                for (int j = -x; j <= x; j++)
                    img->setPixel(QPoint(col+i, row+j), color);
        }
        else
            img->setPixel(QPoint(col, row), color);
    }
}

void pixel::reversePixel(int row, int col, int pixelProportion, QImage *img)
{
    int x;
    if (pixelProportion > 1)
        x = pixelProportion-2;
    else
        x = 0;
    if (col-x > 0 & col+x < img->width()-x & row-x > 0 & row+x < img->height()-x)
    {
        if (qGray(img->pixel(QPoint(col,row))) < 128)
        {
            if (x > 0)
            {
                for (int i = -x; i <= x; i++)
                    for (int j = -x; j <= x; j++)
                        img->setPixel(QPoint(col+i, row+j), qRgb(255,255,255));
            }
            else
                img->setPixel(QPoint(col, row), qRgb(255,255,255));
        }
        else
        {
            if (x > 0)
            {
                for (int i = -x; i <= x; i++)
                    for (int j = -x; j <= x; j++)
                        img->setPixel(QPoint(col+i, row+j), qRgb(0,0,0));
            }
            else
                img->setPixel(QPoint(col, row), qRgb(0,0,0));
        }
    }
}

bool pixel::isPattern(int row, int col, QImage *img)
{
    if (col < 0 || col > img->width()-1 || row < 0 || row > img->height()-1)
        return true;
    if ((img->pixel(QPoint(col,row)) == qRgb(0,255,0)))    //Finder pattern
        return true;
    if ((img->pixel(QPoint(col,row)) == qRgb(232,229,63))) //Timming pattern
        return true;
    if ((img->pixel(QPoint(col,row)) == qRgb(0,0,255)))    //Format pattern
        return true;
    if ((img->pixel(QPoint(col,row)) == qRgb(32,255,200))) //Version pattern
        return true;
    if ((img->pixel(QPoint(col,row)) == qRgb(255,0,0)))    //Alignment pattern
        return true;
    return false;
}
